package utstat;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.TimeUnit;
import static utstat.Team.*;

public class Rcon {


	public static final String R = "^1";
	public static final String G = "^2";
	public static final String Y = "^3";
	public static final String B = "^4";
	public static final String C = "^5";
	public static final String M = "^6";
	public static final String W = "^7";
	
	String passwd;
	String host;
	int port;
	static Rcon rcon;
	private boolean live;
	final ArrayBlockingQueue<String> commandQueue = new ArrayBlockingQueue<String>(100);
	

	private Rcon(String host, String portString, String passwd) {
		this.host = host;
		this.passwd = passwd;
		this.port=Integer.parseInt(portString);
		Thread commandSender = new Thread(new Runnable() {
			byte[] buff = new byte[65507];

			@Override
			public void run() {
				while (true) {
					try {
						String command = null;
						command = commandQueue.poll(1, TimeUnit.MINUTES);
						if(command!=null)
						Q3Tool.q3query("rcon\r" + Rcon.this.passwd + "\r"+command+"\0", buff, Rcon.this.host, port);
						Thread.sleep(550);
					} catch (InterruptedException e) {}
				}

			}
		});
		commandSender.start();
	}

/*	public static void main(String[] args) throws InterruptedException {
		
		Rcon.bigtext(R+"red");
		Rcon.bigtext(B+"blue");
		Rcon.bigtext(G+"green");
		Rcon.bigtext(C+"cyan");
		Rcon.bigtext(Y+"yellow");
		
		Rcon.bigtext(M+"magenta");
		Rcon.bigtext(W+"white");
		
		//Rcon.slap("paul");
		
		Rcon.bigtext(W+"white");
		
		setSpawnTimes(8,9);
	}*/
	
	public static void init(String ip, String port, String password) {
		rcon = new Rcon(ip,port,password);
	}

	private void sendCommand(String command) {
		if(!live) return;
		commandQueue.add(command);
	}

	public static void say(String message) {		
		if(rcon!=null)
		rcon.sendCommand("rcon say "+message);
		System.out.println(message);
	}
	
	public static void slap(String message) {
		
		rcon.sendCommand("rcon slap "+message);
	}
	
	public static void bigtext(String message) {
	
		rcon.sendCommand("rcon bigtext \""+message+"\"");
	}
	
	public static void forceteam(String player, Team team) {
		// workaround, for some reason it doesn't work if you use team.toString()
		String teamname = team==blue?"blue":team==red?"red":"spectator";
		rcon.sendCommand("rcon forceteam "+player+" "+teamname);
	}
	
	public static void setSpawnTimes(int redSpawn,int blueSpawn) {	
		if(redSpawn<2) redSpawn=2; // server crashes when spawn==1
		if(blueSpawn<2) blueSpawn=2;
		rcon.sendCommand("rcon g_bluewave "+blueSpawn);
		rcon.sendCommand("rcon g_redwave "+redSpawn);
		rcon.sendCommand("rcon g_waverespawns 1");
	}
	
	public static void executeSwap(Swap swap) {
		if(swap.getRedPlayer()!=null) forceteam(swap.getRedPlayer().getName(), blue);
		if(swap.getBluePlayer()!=null) forceteam(swap.getBluePlayer().getName(), red);
	}

	public boolean isLive() {
		return live;
	}

	public static void goLive() {rcon.setLive(true);}

	private void setLive(boolean d) {live=d;}

	
}
